<template>
  <div v-if="previewImg.popup" class="rlPopup">
    <div class="close-big-img" @click="close()">
        <i class="el-icon-close" style="color:white;cursor: pointer;font-size:18px"></i>
      <!-- <em class="font_family icon-close-circle"></em> -->
    </div>
    <div class="prev handle-btn" v-if="!isSingle" @click="prev()">
      <em class="font_family icon-left-line"></em>
    </div>
    <div class="next handle-btn" v-if="!isSingle" @click="next()">
      <em class="font_family icon-right--line"></em>
    </div>
    <!--放大缩小旋转-->
    <div class="zoom-wrap">
      <i @click="handleActions('zoomOut')" class="el-icon-zoom-out"></i>
      <i @click="handleActions('zoomIn')" class="el-icon-zoom-in"></i>
      <i @click="handleActions('left')" class="el-icon-refresh-left"></i>
      <i @click="handleActions('right')" class="el-icon-refresh-right"></i>
    </div>
    <div class="img-list-wrap">
      <div v-for="(img, i) in previewImg.imgList" :key="img">
        <img
          ref="img"
          :src="img"
          v-if="i === previewImg.index"
          :style="imgStyle"
          alt="">
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PreviewImage',
  props: {
    previewImg: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      transform: {
        scale: 1,
        degree: 0
      }
    }
  },
  computed: { // 计算属性动态响应数据
    isSingle() { // 单图片无向左向右
      return this.previewImg.imgList.length <= 1
    },
    isFirst() {
      return this.previewImg.index === 0
    },
    isLast() {
      return this.previewImg.index === this.previewImg.imgList.length - 1
    },
    imgStyle() {
      const { scale, degree } = this.transform
      const style = {
        transform: `scale(${scale}) rotate(${degree}deg)`
      }
      return style
    }
  },
  methods: {
    prev() { // 点击切换上一张
      if (this.isFirst && !this.previewImg.infinite) return
      if (this.$parent.companyPreview.index > 0) {
        const len = this.previewImg.imgList.length
        this.$parent.companyPreview.index = (this.$parent.companyPreview.index - 1 + len) % len
        this.reset()
      }
    },
    next() { // 点击切换下一张
      if (this.isLast && !this.previewImg.infinite) return
      if (this.previewImg.imgList.length > this.$parent.companyPreview.index) {
        const len = this.previewImg.imgList.length
        this.$parent.companyPreview.index = (this.$parent.companyPreview.index + 1) % len
        this.reset()
      }
    },
    handleActions(action) { // 放大旋转等操作
      const { zoomRate, rotateDeg, enableTransition } = {
        zoomRate: 0.2,
        rotateDeg: 90,
        enableTransition: true
      }
      const { transform } = this
      switch (action) {
        case 'zoomOut':
          if (transform.scale > 0.2) {
            transform.scale = parseFloat((transform.scale - zoomRate).toFixed(3))
          }
          break
        case 'zoomIn':
          transform.scale = parseFloat((transform.scale + zoomRate).toFixed(3))
          break
        case 'left':
          transform.degree -= rotateDeg
          break
        case 'right':
          transform.degree += rotateDeg
          break
      }
      transform.enableTransition = enableTransition
    },
    reset() { // 初始化图片样式
      this.transform = {
        scale: 1,
        degree: 0
      }
    },
    close() { // 父组件隐藏
      this.$parent.companyPreview.popup = false
    }
  }
}
</script>

<style lang="scss" scoped>
  // 查看大图
  .rlPopup { // 全屏遮罩显示
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 10000;
    .close-big-img {
      position: absolute;
      top: 20px;
      right: 5%;
      z-index: 10000;

      em {
        cursor: pointer;
        color: #ffffff;
        font-size: 36px;
      }
    }

    .handle-btn {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      width: 44px;
      height: 44px;
      line-height: 38px;
      font-size: 24px;
      color: #fff;
      background-color: #606266;
      border-color: #fff;
      border-radius: 50%;
      cursor: pointer;
      text-align: center;
      z-index: 10000;
    }

    .prev {
      left: 40px;
    }

    .next {
      right: 40px;
    }

    .img-list-wrap {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        display: block;
        object-fit: scale-down;
        transition: all 0.3s;
        // width: 480px;
        // height: 320px;
      }
    }

    .zoom-wrap {
      position: absolute;
      bottom: 16px;
      left: 50%;
      transform: translateX(-50%);
      background-color: #606266;
      border-radius: 22px;
      z-index: 10000;
      width: 222px;
      height: 44px;
      line-height: 47px;
      padding: 0 23px;

      i {
        color: #ffffff;
        cursor: pointer;
        font-size: 22px;
        margin-right: 25px;

        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
</style>
